package com.ksd.modules.course.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ksd.common.enums.DeletedEnums;
import com.ksd.common.web.threadlocal.SystemIdThrealLocal;
import com.ksd.commons.BaseServiceImpl;
import com.ksd.convert.CopyConvertUtils;
import com.ksd.modules.course.entity.AdminCourse;
import com.ksd.modules.course.entity.AdminCourseCategory;
import com.ksd.modules.course.mapper.AdminCourseCategoryMapper;
import com.ksd.modules.course.mapper.AdminCourseMapper;
import com.ksd.modules.course.po.AdminCourseCategorySavePO;
import com.ksd.modules.course.po.AdminCourseCategorySearchPO;
import com.ksd.modules.course.service.IAdminCourseCategoryService;
import com.ksd.modules.course.vo.AdminCourseCategoryVo;
import com.ksd.modules.system.enums.DefaultEnums;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class AdminCourseCategoryServiceImpl extends BaseServiceImpl<AdminCourseCategoryMapper, AdminCourseCategory> implements IAdminCourseCategoryService {

    @Resource
    private AdminCourseMapper adminCourseMapper;
    /**
     * 查询菜单接口搜索并分页
     *
     * @return
     */
    @Override
    public List<AdminCourseCategoryVo> findAdminCourseCategoryRoot() {
        LambdaQueryWrapper<AdminCourseCategory> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AdminCourseCategory::getSystemId,SystemIdThrealLocal.get().getSystemId());
        lambdaQueryWrapper.eq(AdminCourseCategory::getIsDeleted, DeletedEnums.UN_DELETED.getCode());
        lambdaQueryWrapper.eq(AdminCourseCategory::getParentId, DefaultEnums.NO.getCode());
        lambdaQueryWrapper.orderByAsc(AdminCourseCategory::getSorted);
        return CopyConvertUtils.copyCollection(this.list(lambdaQueryWrapper), AdminCourseCategoryVo.class);
    }

    @Override
    public List<AdminCourseCategoryVo> findAdminCourseCategory(AdminCourseCategorySearchPO adminCourseCategorySearchPO) {
        LambdaQueryWrapper<AdminCourseCategory> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(adminCourseCategorySearchPO.getKeyword())) {
            lambdaQueryWrapper.and(c -> c.like(AdminCourseCategory::getTitle, adminCourseCategorySearchPO.getKeyword()));
        }
        lambdaQueryWrapper.eq(Objects.nonNull(adminCourseCategorySearchPO.getStatus()), AdminCourseCategory::getStatus, adminCourseCategorySearchPO.getStatus());
        lambdaQueryWrapper.eq(AdminCourseCategory::getSystemId,SystemIdThrealLocal.get().getSystemId());
        lambdaQueryWrapper.eq(AdminCourseCategory::getIsDeleted, DeletedEnums.UN_DELETED.getCode());
        lambdaQueryWrapper.orderByAsc(AdminCourseCategory::getSorted);
        //数据查询
        List<AdminCourseCategory> list = baseMapper.selectList(lambdaQueryWrapper);
        List<AdminCourseCategoryVo> adminCourseCategoryVos = CopyConvertUtils.copyCollection(list, AdminCourseCategoryVo.class);
        //新建一个用于接收数据的list
        List<AdminCourseCategoryVo> resultList = new ArrayList<>();
        for (AdminCourseCategoryVo result : adminCourseCategoryVos) {
            if (result.getParentId() .equals(0L) ) {
                result.setPostCount(countAdminCourse(result.getId(),null,result.getSystemId()).intValue());
                //调用方法给子类添加数据
                resultList.add(getAdminCourseCategoryTree(result, adminCourseCategoryVos));
            }
        }
        return resultList;
    }

    /**
     * 统计分类的资源数量
     *
     * @param pid
     * @param cid
     * @return
     */
    public Long countAdminCourse(Long pid, Long cid,Long systemId) {
        LambdaQueryWrapper<AdminCourse> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AdminCourse::getCategoryPid, pid);
        lambdaQueryWrapper.eq(Objects.nonNull(cid), AdminCourse::getCategoryId, cid);
        lambdaQueryWrapper.eq(AdminCourse::getSystemId,systemId);
        lambdaQueryWrapper.eq(AdminCourse::getIsDeleted, DeletedEnums.UN_DELETED.getCode());
        return adminCourseMapper.selectCount(lambdaQueryWrapper);
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean saveUpdateAdminCourseCategory(AdminCourseCategorySavePO adminCourseCategorySavePO) {
        AdminCourseCategory adminDownloadsCategory = CopyConvertUtils.copyProperties(adminCourseCategorySavePO, AdminCourseCategory.class);
        adminDownloadsCategory.setSystemId(SystemIdThrealLocal.get().getSystemId());
        adminDownloadsCategory.setSystemName(SystemIdThrealLocal.get().getSystemName());
        return this.saveOrUpdate(adminDownloadsCategory);
    }
  
    @Override
    public boolean deleteAdminCourseCategoryById(Long id) {
        return this.removeById(id);
    }

    @Override
    public AdminCourseCategoryVo getAdminCourseCategoryById(Long id) {
        return CopyConvertUtils.copyProperties(this.getById(id), AdminCourseCategoryVo.class);
    }

    /**
     * 递归生成板块
     *
     * @param result
     * @param list
     * @return
     */
    private AdminCourseCategoryVo getAdminCourseCategoryTree(AdminCourseCategoryVo result, List<AdminCourseCategoryVo> list) {
        for (AdminCourseCategoryVo menu : list) {
            //如果父类主键等于传过来实体类的ID
            if (Objects.equals(menu.getParentId(), result.getId())) {
                menu.setPostCount(countAdminCourse(result.getId(),menu.getId(),menu.getSystemId()).intValue());
                if (result.getChildren() == null) {
                    result.setChildren(new ArrayList<>());
                }
                // 递归调用
                result.getChildren().add(getAdminCourseCategoryTree(menu, list));
            }
        }
        return result;
    }

    /**
     * 复制
     * @param opid
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean copyData(Long opid){
        AdminCourseCategory data = this.getById(opid);
        if(Objects.nonNull(data)){
            data.setId(null);
            data.setStatus(0);
            data.setCreateTime(LocalDateTime.now());
            data.setUpdateTime(LocalDateTime.now());
            data.setTitle(data.getTitle()+"[副本]");
            this.save(data);
        }
        return true;
    }
}
